package meconsea.life.cms.web.tags;

import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import meconsea.life.cms.accessor.SArticleAccessor;
import meconsea.life.cms.accessor.VArticleAccessor;
import meconsea.life.cms.accessor.XArticleAccessor;
import meconsea.life.cms.accessor.YArticleAccessor;
import meconsea.life.cms.accessor.ZArticleAccessor;
import meconsea.life.cms.entity.Article;
import meconsea.life.cms.entity.SArticle;
import meconsea.life.cms.entity.VArticle;
import meconsea.life.cms.entity.XArticle;
import meconsea.life.cms.entity.YArticle;
import meconsea.life.cms.entity.ZArticle;
import meconsea.life.util.Constant;
import meconsea.life.util.GuiceUtil;

import org.apache.log4j.Logger;
import org.apache.struts2.components.Component;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 该标签实现网站、各分类的首页最新置顶文章
 * @author Administrator
 *
 */
public class ArtHeader extends Component {
	private static final Logger log = Logger.getLogger(ArtHeader.class);
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected PageContext pageContext;
	public ArtHeader(ValueStack stack, HttpServletRequest req,
			HttpServletResponse res, PageContext pc) {
		super(stack);
		this.request = req;
		this.response = res;
		this.pageContext = pc;
	}
	//业务数据
	//文章类型 0:全部 y:衣 s:食 z:住 x:行
	private String artType;
	//文章分类 不包含子类
	private String artCls;
	public String getArtType() {
		return artType;
	}
	public void setArtType(String artType) {
		this.artType = artType;
	}
	public String getArtCls() {
		return artCls;
	}
	public void setArtCls(String artCls) {
		this.artCls = artCls;
	}
	
	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		try{ 
			StringBuffer s_b = new StringBuffer("");
			Article art = null;
			if(Constant.ART_TYPE_ALL.equals(artType)){
				VArticleAccessor va = (VArticleAccessor)GuiceUtil.getInstance().getInstanceClass(VArticleAccessor.class);
				List<VArticle> lv = va.getHeaderArt(Constant.ART_HEADER, 0, 1);
				art = lv.get(0);
			}else if(Constant.ART_TYPE_S.equals(artType)){
				SArticleAccessor sa = (SArticleAccessor)GuiceUtil.getInstance().getInstanceClass(SArticleAccessor.class);
				List<SArticle>  ls = sa.getHeaderArt(Constant.ART_HEADER, 0, 1);
				art = ls.get(0);
			}else if(Constant.ART_TYPE_X.equals(artType)){
				XArticleAccessor xa = (XArticleAccessor)GuiceUtil.getInstance().getInstanceClass(XArticleAccessor.class);
				List<XArticle>  lx = xa.getHeaderArt(Constant.ART_HEADER, 0, 1);
				art = lx.get(0);				
			}else if(Constant.ART_TYPE_Y.equals(artType)){
				YArticleAccessor ya = (YArticleAccessor)GuiceUtil.getInstance().getInstanceClass(YArticleAccessor.class);
				List<YArticle>  ly = ya.getHeaderArt(Constant.ART_HEADER, 0, 1);
				art = ly.get(0);
			}else if(Constant.ART_TYPE_Z.equals(artType)){
				ZArticleAccessor za = (ZArticleAccessor)GuiceUtil.getInstance().getInstanceClass(ZArticleAccessor.class);
				List<ZArticle>  lz = za.getHeaderArt(Constant.ART_HEADER, 0, 1);
				art = lz.get(0);
			}else{
				return false;
			} 
			if(art != null){	 
				s_b.append("<a href='"+Constant.ART_LINK_BASE+"?id="+art.getId()+"' target='_blank'>");
				s_b.append(art.getSubtitle());
				s_b.append("</a>");
			}else{
				return false;
			}
			writer.write(s_b.toString());
		}catch(Exception e){
			log.error("获取标题文章出现异常：",e);
			return false;
		}
		return result;
	}
	
}
